function renderGetgory() {
    axios.get('/my/category/list').then(res => {
        // console.log(res);
        let { status, data } = res.data;
        if (status === 0) {
            let str = '';
            data.forEach(item => {
                str +=
                    `
                     <tr>
                     <td>${item.name}</td>
                     <td>${item.alias}</td>
                     <td>
                       <button "type="button" class="layui-btn layui-btn-xs" id="edit"data-id="${item.id}" data-name="${item.name}" data-alias="${item.alias}">编辑</button>
                       <button data-id="${item.id}" type="button" class="del layui-btn layui-btn-xs layui-btn-danger">删除</button>
                     </td>
                   </tr>
                     `

            })
            $('tbody').html(str);
        }
    });
}
renderGetgory();
let addIndex;
$('#add').on('click', function () {
    addIndex = layer.open({
        type: 1,
        title: '添加类别',
        content: $('#tpl-add').html(), // 内容在HTML中
        area: ['500px', '250px']
    });

});
// add发送请求
$('body').on('submit', '#add-form', function (e) {
    e.preventDefault();
    let data = $(this).serialize();
    axios.post('/my/category/add', data).then(res => {
        let { status, message } = res.data;
        if (status === 0) {
            layer.msg(message)
            renderGetgory();
            layer.close(addIndex);
        }
    })
})
// delete 发送请求
$('tbody').on('click', '.del', function () {
    let id = $(this).data('id');
    layer.confirm('你确定删除了吗?', { icon: 3, title: '提示' }, function (index) {
        //do something
        axios.get('/my/category/delete?id=' + id + "",).then(res => {
            let { status, message } = res.data;
            if (status === 0) {
                layer.msg(message)
                renderGetgory();
            }
        })
        layer.close(index);
    });

});
// 编辑分类
// 数据回填
let editIndex;
$('tbody').on('click', '#edit', function () {
    let shuju = $('#edit').data();
    editIndex = layer.open({
        type: 1,
        title: '编辑类别',
        content: $('#tpl-edit').html(), 
        area: ['500px', '250px'],
        success : function () {
          let form = layui.form ;
          form.val('edit',shuju)
        }
    });

});
// 修改
$('body').on('submit','#edit-form',function (e) {
    e.preventDefault();
    let data = $(this).serialize();
    axios.post('/my/category/update',data).then( res => {
        let { status, message } = res.data;
        if ( status === 0) {
            layer.msg(message);
            renderGetgory();
            layer.close(editIndex)
        }
    })
});